/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test_system.menu;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import test_system.menu.language.*;
import test_system.controller.TestController;
import test_system.user_logic.*;

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import test_system.parser.ParserCSV;
import test_system.questions.Answer;
import test_system.questions.QuestionWrapper;
import test_system.questions.TestWrapper;

/**
 *
 * @author root
 */
public class Menu {

    private Scanner in = new Scanner(System.in);
    static Language language;

    public Session avtorization() {
        checkLanguage();
        Session session = null;
        int tryCounter = 0;
        String[] avtorizationData = language.avtorization();
        do {
            if (tryCounter == 3) {
                System.out.println("The programm will close.");
                System.exit(0);
            }
            session = userAvtorization(avtorizationData, session);
            System.out.println("admin" + session.isAdmin());
            tryCounter++;
        } while (session == null);
        return session;
    }

    public void adminMenu() {
        if (TestController.userSession.isAdmin()) {
            int i;
            do {
                System.out.println("Input user login to check statistic");
                String login = in.nextLine();
                ((Admin) TestController.userSession.getUser()).seeStatistic(login);
                System.out.println("If you want to repeat? press 1");
                i = in.nextInt();
            } while (i == 1);
        }
        System.exit(0);
    }

    private Session userAvtorization(String[] avtorizationData, Session session) {
        System.out.println(avtorizationData[0]);
        System.out.println(avtorizationData[1]);
        String login = in.nextLine();
        System.out.println(avtorizationData[2]);
        String password = in.nextLine();
        session = Session.createSession(login, password);
        return session;
    }

    public ArrayList<Answer> getUserAnswers(ArrayList<Answer> answers, int index) {
        QuestionWrapper question;
        for (int i = index; i < TestController.test.getTestLength();) {
            question = outputQuestion(i);
            String ans = in.nextLine();
            if (ans.equals("s")) {
                SessionSaver ss = new SessionSaver(TestController.userSession.getUser(), TestController.test, answers);
                ss.save();
            } else if (ans.equals("undo")) {
                i--;
                continue;
            } else if (ans.equals("redo")) {
                i++;
                continue;
            }
            answers.add(conventUserAnswer(ans, question));
            i++;
        }
        return answers;
    }

    Answer conventUserAnswer(String stringAnswer, QuestionWrapper question) {
        return new Answer(stringAnswer, stringAnswer.equals(question.getRigthAnswerToCheck()));
    }

    public void markOutput(UserAnswers userAnswers) {
        String markOutout = "%1$s %2$d/%3$d";
        int mark[] = userAnswers.evaluation();
        saveResult(mark);
        System.out.println(String.format(markOutout, language.markOutout(), mark[0], mark[1]));
    }

    public void saveResult(int[] mark) {
        String fileName = "w1_statistic.txt";
        File f = new File(fileName);
        StringBuffer statistic;
        String markString = mark[0] + "/" + mark[1];
        System.out.println("mark = " + markString);
        if (f.exists()) {
            ParserCSV parser = new ParserCSV();
            statistic = parser.parseFile(fileName);
        } else {
            try {
                f.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
            }
            statistic = new StringBuffer();
        }
        statistic.append(markString);
        try {
            FileWriter wrt = new FileWriter(f);
            wrt.append(statistic);
            wrt.flush();
        } catch (IOException e) {
            System.out.println("there is some trouble with write-function");
            e.printStackTrace();
        }

    }

    QuestionWrapper outputQuestion(int index) {
        System.out.println(language);
        String[] languageContext = language.getQuestionContext();
        System.out.print("To save and exit press \"s\". ");
        String fullQuestion = "%1$s\n%2$s";
        if (TestController.test.getQuestion(index).getClassName().equals("TextAnswerQuestion")) {
            System.out.println(String.format(fullQuestion, languageContext[0], TestController.test.getQuestion(index).toString()));
        } else {
            System.out.println(String.format(fullQuestion, languageContext[1], TestController.test.getQuestion(index).toString()));
        }
        return TestController.test.getQuestion(index);
    }

    private void checkLanguage() {
        String lan;
        do {
            System.out.println("Please, check your language:\n1)English\n2)Українська\n3)Русский");
            lan = in.nextLine();
            if (lan.equals("1")) {
                language = new EnLanguage();
            } else if (lan.equals("2")) {
                language = new UaLanguage();
            } else if (lan.equals("3")) {
                language = new RuLanguage();
            } else {
                System.out.println("Check right answer.");
            }
        } while (!(lan.equals("1") || lan.equals("2") || lan.equals("3")));
    }
}
